﻿#ifndef CHATMANAGE_H
#define CHATMANAGE_H

#include "tcp/threadforcurl.h"
#include "tcp/threadforloading.h"
#include "tcp/serverthread.h"
#include "tcp/threadworker.h"
#include "allsqlconversationmodel.h"
#include "sqlcontactmodel.h"

#include <QSharedPointer>
#include <QObject>
#include <QString>
#include <memory>
#include <QTimer>

class ChatManage:public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool Registe READ Registe WRITE Registe NOTIFY registeChanged)
    Q_PROPERTY(bool Network READ Network WRITE Network NOTIFY networkChanged)
    Q_PROPERTY(QJsonObject MyInfo READ MyInfo WRITE MyInfo NOTIFY MyInfoChanged)
    Q_PROPERTY(QJsonObject MyConfig READ MyConfig WRITE MyConfig NOTIFY MyConfigChanged)
    Q_PROPERTY(QString MyPi READ MyPi NOTIFY myPiChanged)
//    Q_PROPERTY(QColor BackGround READ BackGround WRITE BackGround NOTIFY BackGroundChanged)
//    Q_PROPERTY(QColor TextColor READ TextColor WRITE TextColor NOTIFY TextColorChanged)
public:
    explicit ChatManage( QObject *param = nullptr,AllSqlConversationModel *psql = nullptr,SqlContactModel *pcont = nullptr);
    ~ChatManage();

    Q_INVOKABLE void sendRegiste(const QString &user_mail,const QString &user_pwd);
//    Q_INVOKABLE QString getMyInfo(const QString &key);

    Q_INVOKABLE void sendTCP(QString type,QString version,QString confirm,QString state,QString other);

    Q_INVOKABLE QString getConfigValue(QString key);
    Q_INVOKABLE void writeConfigValue(QString key,QString Value);
    Q_INVOKABLE void exitCurrentUser();

    Q_INVOKABLE void setUserConfig(QString name,QString pwd,QString img);

    Q_INVOKABLE bool isExit(){return m_exit;}

    Q_INVOKABLE void openTempFile(const QString &path);

    Q_INVOKABLE void copyString(QString str);
    Q_INVOKABLE QString copyFile(QString path);

    Q_INVOKABLE QString copyFileToPath(QString source,QString des,bool coverFileIfExist);

    Q_INVOKABLE void sendGetPiIp();

    Q_INVOKABLE void test();

    QString MyPi();
//    Q_INVOKABLE QString forceToUpdate(int type,QString ip);
    QString GetCorrectUnicode(const QByteArray &ba);
public:

    bool Registe(){
        return m_Registe;
    }

    void Registe(bool value){
        m_Registe = value;
        emit registeChanged();
        if(!m_Registe){
            if(m_sqlContact)
                m_sqlContact->allUserOffline();
        }
    }

    bool Network(){
        return m_Network;
    }

    void Network(bool value){
        m_Network = value;
        if(value){
            if(m_exit)
                return;
            if(!m_MyInfo.value("user_mail").toString().isEmpty()){
                sendRegiste(m_MyInfo.value("user_mail").toString(),m_MyInfo.value("user_pwd").toString());
            }
        }
        else{
            Registe(value);
        }
        emit networkChanged();
    }

    QJsonObject MyInfo(){return m_MyInfo;}
    void MyInfo(QJsonObject value){m_MyInfo = value;emit MyInfoChanged();}

    QJsonObject MyConfig(){return m_MyConfig;}
    void MyConfig(QJsonObject value){m_MyConfig = value;emit MyConfigChanged();}

    QJsonObject getOneUserData(const QString &listvalue);
    QString formateQJsonObject(const QJsonObject &jsonvalue);
    void initRegiste(const QJsonObject &jsonvalue);
//    QColor BackGround(){
//        return m_background;
//    }

//    void BackGround(QColor color){
//        m_background = color;
//        emit BackGroundChanged();
//    }
//    QColor TextColor(){
//        return m_textcolor;
//    }
//    void TextColor(QColor color){
//        m_textcolor = color;
//        emit TextColorChanged();
//    }
public slots:
//    void addtoUpdateList(QJsonObject);
    void MessageRev(int type,int subtype,QString str);
    void SocketStateChange(int state,QString str);

    void getData(int hd,int code,QString answer);
    void error(int hd,int socketError, const QString &message);


    void toGetError(int type,int subIndex, QString message);

    void sendToServer(int type,QString value);

    void uploadFile(QJsonObject value);
    void downloadFile(QJsonObject value);

signals:
    void getError(int type,int subIndex, QString message);
    void loginAnswer(bool value,QString errMsg);

//    void senddata(const QByteArray &data);

    void registeChanged();
    void networkChanged();
    void MyInfoChanged();
    void MyConfigChanged();

    void sendData(const QString &);

    void BackGroundChanged();
    void TextColorChanged();

    void sendToSql(QJsonObject);
    void sendToSqlFile(QJsonObject);
    void myPiChanged();
private:
    void package(int type,QString value);
    QStringList getHostIpMac();
    int generateRandom(int min=0,int max=10000){
        Q_ASSERT(min<max);
        if(m_random<max){
            m_random++;
        }
        else{
            m_random=min;
        }
        return m_random;
    }

    QString m_user;
    int m_random;

    QTimer *pTimer;
    int m_time;

    QTimer *pRegisteDelay;

    QString serIP;
    quint16 serPort;
    quint16 serPortF;

//    serverthread *pser;
    Controller *p_Contrel;
//    QPointer<socketsend> psend;

    bool m_Registe;
    bool m_Network;

//    QMap<QString,QJsonObject> m_onlineUser;
    QJsonObject m_MyInfo;
    QJsonObject m_MyInfo_Temp;

    QJsonObject m_MyConfig;
//    LView *m_view;
    AllSqlConversationModel *m_sqlModel;
    SqlContactModel *m_sqlContact;
    bool m_exit;
//    QColor m_background;
//    QColor m_textcolor;
    QString m_piIp;

    QSharedPointer<Curl::Controller> m_Curl;
    QSharedPointer<HttpLoading::Controller> m_DownLoad;
};

#endif // CHATMANAGE_H
